
from utils.utils import *
import allure
import pytest
from faker import Faker
faker = Faker(locale="zh_CN")
name=faker.name()

@allure.story("添加话术")

def test_add_speech():
    reset_token()

    # 创建请求步骤
    add_speech = send_requests(
        "新增话术",
        "post",
        "/adminapi/app/wechat/speechcraft",
        {"cate_id":0,"title":"1","message":f"{name}","sort":0}  # 使用 name 变量
    ).\
    validate().\
    assert_equal("status_code", 200).\
    assert_equal('headers."Content-Type"', "application/json; charset=utf-8").\
    assert_equal("body.status", 200).\
    assert_equal("body.msg", "创建话术成功").\
    assert_equal("body.code", "400270")

    # 执行请求
    add_speech_step = step_obj(add_speech)
    res = get_last_response([add_speech_step])


@allure.story("修改话术")
def test_modify_Speech():

    reset_token()
    get_speech = send_requests('查询话术', 'get', '/adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id='). \
        validate(). \
        assert_equal("status_code", 200). \
        assert_equal('headers."Content-Type"', "application/json; charset=utf-8"). \
        assert_equal("body.status", 200). \
        assert_equal("body.msg", "success")
    get_speech_step = step_obj(get_speech)
    res = get_last_response([get_speech_step, ])

    for i in res['res_body']['data']['list']:
        if i['message'] == name:
            id = i['id']
    #修改话术
    update_speech = send_requests(
        '修改话术',
        'put',
        f'/adminapi/app/wechat/speechcraft/{id}',
        {"cate_id":0,"title":f"{name}","message":f"{name}","sort":0}


    ).\
    validate(). \
    assert_equal("status_code", 200).\
    assert_equal('headers."Content-Type"', "application/json; charset=utf-8").\
    assert_equal("body.status", 200).\
    assert_equal("body.msg", "修改成功").\
    assert_equal("body.code", "100001")
    update_speech_step = step_obj(update_speech)
    res = get_last_response([update_speech_step,])

@allure.story("删除话术")
def test_delSpeech():
    reset_token()
    get_speech = send_requests('查询话术', 'get', '/adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id='). \
    validate(). \
    assert_equal("status_code", 200). \
    assert_equal('headers."Content-Type"', "application/json; charset=utf-8"). \
    assert_equal("body.status", 200). \
    assert_equal("body.msg", "success")
    get_speech_step = step_obj(get_speech)
    res = get_last_response([get_speech_step, ])

    for i in res['res_body']['data']['list']:
        if i['message'] == name:
            id = i['id']
    #删除
    delete_Speech = send_requests(
        "删除话术",
        'delete',
        f"/adminapi/app/wechat/speechcraft/{id}"
    ). \
    validate().\
    assert_equal("status_code", 200).\
    assert_equal('headers."Content-Type"', "application/json; charset=utf-8").\
    assert_equal("body.status", 200).\
    assert_equal("body.msg", "删除成功").\
    assert_equal("body.code", "100002")
    delete_Speech_step = step_obj(delete_Speech)
    res = get_last_response([delete_Speech_step,])







